﻿using HtmlAgilityPack;
using System.Net;
using System.Text;

namespace apitest.Application
{
    public class SpiderService 
    {
        public async Task<dynamic> getContent(string url)
        {
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            HtmlDocument doc = new HtmlDocument();
            string html = wc.DownloadString(url);

            doc.LoadHtml(html);
            var nodes = doc.DocumentNode;
            var title = nodes.SelectNodes("//h1");
            var str = title[0].InnerText + "\r\n";
            var list = nodes.SelectNodes("//p[@class='content_detail']");
            foreach (var item in list)
            {
                var txt = item.InnerText.Replace("\r\n", "").Replace(" ", "");
                str = str + txt + "\r\n";
            }


            return str;
        }

        public async Task<dynamic> GetTitleList()
        {
            var url = "http://www.xswang.org/book/66379/";
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            HtmlDocument doc = new HtmlDocument();
            string html = wc.DownloadString(url);
            doc.LoadHtml(html);
            var nodes = doc.DocumentNode;
            var list = nodes.SelectNodes("//a");
            var allText = string.Empty;
            var wrongText = string.Empty;
            for (int i = 662; i < 710; i++)
            {

                var a = 转换万以下整数(i.ToString());
                await Console.Out.WriteLineAsync($"========================={i}========{a}=================");
                var titleStr = $"第{转换万以下整数(i.ToString())}章";
                var linkA = list.FirstOrDefault(o => o.InnerText.Contains(titleStr));
                if (linkA != null && linkA.Attributes.Count > 0)
                {
                    var url1 = $"http://www.xswang.org{linkA.Attributes[0].DeEntitizeValue}";
                    var txt = await getContent(url1);
                    allText += txt;
                    File.WriteAllText(@$"C:\123\222\{i}.txt", txt);
                }
                else
                {
                    wrongText += titleStr + "\r\n";
                }

                Thread.Sleep(1000);
            }
            File.WriteAllText(@$"C:\123\222\all.txt", allText);
            File.WriteAllText(@$"C:\123\222\wrong.txt", wrongText);
            return list.Count;
        }


        private char ToNum(char x)
        {
            string strChnNames = "零一二三四五六七八九";
            string strNumNames = "0123456789";
            return strChnNames[strNumNames.IndexOf(x)];
        }

        private string 转换万以下整数(string x)
        {
            string[] stringArrayLevelNames = new string[4] { "", "十", "百", "千" };
            string ret = "";
            int i;
            for (i = x.Length - 1; i >= 0; i--)
                if (x[i] == '0')
                    ret = 转换数字(x[i]) + ret;
                else
                    ret = 转换数字(x[i]) + stringArrayLevelNames[x.Length - 1 - i] + ret;
            while ((i = ret.IndexOf("零零")) != -1)
                ret = ret.Remove(i, 1);
            if (ret[ret.Length - 1] == '零' && ret.Length > 1)
                ret = ret.Remove(ret.Length - 1, 1);
            if (ret.Length >= 2 && ret.Substring(0, 2) == "一十")
                ret = ret.Remove(0, 1);
            return ret;
        }
        private string 转换整数(string x)
        {
            int len = x.Length;
            string ret, temp;
            if (len <= 4)
                ret = 转换万以下整数(x);
            else if (len <= 8)
            {
                ret = 转换万以下整数(x.Substring(0, len - 4)) + "万";
                temp = 转换万以下整数(x.Substring(len - 4, 4));
                if (temp.IndexOf("千") == -1 && temp != "")
                    ret += "零" + temp;
                else
                    ret += temp;
            }
            else
            {
                ret = 转换万以下整数(x.Substring(0, len - 8)) + "亿";
                temp = 转换万以下整数(x.Substring(len - 8, 4));
                if (temp.IndexOf("千") == -1 && temp != "")
                    ret += "零" + temp;
                else
                    ret += temp;
                ret += "万";
                temp = 转换万以下整数(x.Substring(len - 4, 4));
                if (temp.IndexOf("千") == -1 && temp != "")
                    ret += "零" + temp;
                else
                    ret += temp;
            }
            int i;
            if ((i = ret.IndexOf("零万")) != -1)
                ret = ret.Remove(i + 1, 1);
            while ((i = ret.IndexOf("零零")) != -1)
                ret = ret.Remove(i, 1);
            if (ret[ret.Length - 1] == '零' && ret.Length > 1)
                ret = ret.Remove(ret.Length - 1, 1);
            return ret;
        }

        private char 转换数字(char x)
        {
            string stringChnNames = "零一二三四五六七八九";
            string striumNames = "0123456789";
            return stringChnNames[striumNames.IndexOf(x)];
        }


        public async Task<dynamic> Merge()
        {
            var path = @"C:\123\222";
            var dirInfo = new DirectoryInfo(path);
            var files = dirInfo.GetFiles();
            var wrongText = string.Empty;
            var allText = string.Empty;
            for (int i = 50; i < 662; i++)
            {
                var name = $"{i}.txt";
                var file = files.FirstOrDefault(o => o.Name == name);
                if (file != null)
                {

                    allText += System.IO.File.ReadAllText(file.FullName);
                }
                else
                {
                    wrongText = wrongText + name + " ";
                }

                //var name = $"{i}.txt";
                //if (!files.Any(o=>o.Name==name))
                //{
                //    wrongText = wrongText + name + " ";
                //}
            }
            File.WriteAllText(@$"C:\123\aaa.txt", allText);
            return wrongText;
        }
    }
}
